import { getResult } from './util'
import XLSX from 'xlsx'
var rABS = false //是否将文件读取为二进制字符串
export function importf (f, _this) {//导入
  // if (!obj.files) { return; }
  // var f = obj.files[0];
  {
    var reader = new FileReader()
    var name = f.name
    reader.onload = function (e) {
      var data = e.target.result
      var wb
      if (rABS) {
        wb = XLSX.read(data, { type: 'binary' })
      } else {
        var arr = fixdata(data)
        wb = XLSX.read(btoa(arr), { type: 'base64' })
      }
      // console.log(wb.SheetNames)
      const downLoadData = getResult(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]));
      _this.downLoadData = downLoadData
      // _this.bindDownload()
      // console.log(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]))
      // console.log(getResult(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])))
    }
    if (rABS) {
      reader.readAsBinaryString(f)
    } else {
      reader.readAsArrayBuffer(f)
    }
  }
}
function fixdata (data) {
  var o = '',
    l = 0,
    w = 10240
  for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
  o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
  return o;
}
